package com.joshua.viewtest.demo.ExpandingView;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.diegodobelo.expandingview.ExpandingItem;
import com.diegodobelo.expandingview.ExpandingList;
import com.joshua.viewtest.R;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ExpandingDemoAty extends AppCompatActivity {
    @BindView(R.id.expandList)
    ExpandingList list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expanding_demo_aty);
        ButterKnife.bind(this);

        createItems();
    }

    private void createItems() {
        final String[] subItems = {"经度", "纬度", "速度", "高程"};
        //Let's create an item with R.layout.expanding_layout
        final ExpandingItem item = list.createNewItem(R.layout.demo_expanding_layout);

        //If item creation is successful, let's configure it
        if (item != null) {
            item.setIndicatorColorRes(R.color.pink);
            item.setIndicatorIconRes(R.drawable.ic_ghost);
            //It is possible to get any view inside the inflated layout. Let's set the text
            // in the item
//            ((TextView) item.findViewById(R.id.title)).setText(title);

            //We can create items in batch.
            item.createSubItems(4);
            for (int i = 0; i < item.getSubItemsCount(); i++) {
                //Let's get the created sub item by its index
                final View view = item.getSubItemView(i);

                //Let's set some values in
                configureSubItem(item, view, subItems[i]);
            }
        }
    }

    private void configureSubItem(final ExpandingItem item, final View view, String subTitle) {
        ((TextView) view.findViewById(R.id.sub_title)).setText(subTitle);
    }

    private void showInsertDialog(final ExpandingAty.OnItemCreated positive) {
        final EditText text = new EditText(this);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(text);
        builder.setTitle(R.string.enter_title);
        builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                positive.itemCreated(text.getText().toString());
            }
        });
        builder.setNegativeButton(android.R.string.cancel, null);
        builder.show();
    }
}
